package cn.edu.lzu.unitweb.controller;

import cn.edu.lzu.common.assist.DataPage;
import cn.edu.lzu.entity.ExamineItem;
import cn.edu.lzu.entity.Examiner;
import cn.edu.lzu.entity.Permission;
import cn.edu.lzu.service.PermissionService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.security.Security;
import java.util.List;

/**
 * @PackageName: cn.edu.lzu.unitweb.controller
 * @ClassName: PermissionController
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2020/5/5 18:00
 * @Version: 1.0
 */
@Controller
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    @ResponseBody
    @RequestMapping("/getPermissions")
    public DataPage getPermissions(){
        return permissionService.getPermissions();
    }

    @ResponseBody
    @RequestMapping("/getPermissionsByRoleId")
    public List<Permission> getPermissionsByRoleId(int roleId){
        Session session= SecurityUtils.getSubject().getSession();
        Examiner examiner=(Examiner)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        List<Permission> permissionList=permissionService.getPermissionsByRoleId(examiner.getUnitId(), roleId);
        return permissionList;
    }
}
